<?php

namespace App\Manage;

use App\Models\Admin;
use App\Service\Base\Manage;

class AdminManage extends Manage
{
    protected $model = 'admin';

    // 管理员管理
    public function initialize()
    {
        parent::initialize();

        $this->model = 'admin';
        $this->option()->assign('title', '管理员管理');
    }

    // 列表查询
    protected function listWhere()
    {
        $option   = $this->option();
        $where    = [['status', '>=', 0]];
        $bang     = [];
        $username = $option->getData('username', null, 'trim');
        $keyword  = $option->getData('keyword', null, 'trim');
        if($keyword){
            $where[] = $this->orWhere('username|nickname|mobile', 'like', $keyword);
            $bang    = false; // 设置不在缓存
        }
        if($username){
            $where[] = $this->orWhere('username', 'like', $username);
            $bang    = false; // 设置不在缓存
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }


    // 修改密码
    public function on_password()
    {
        $password = $this->getData('password', '123456', 'trim');
        $model    = new Admin();
        $data     = ['password' => $password,];
        $data     = $model->checkData($data, ['id' => $this->id,]);
        $this->throwIf($data === false, $model->getError());

        $where  = $this->editWhere();
        $result = $model->oneEdit($where, $data);
        $this->throwIf($result === false, $model->getError());

        return $this->success();
    }

    // 获取权限
    public function on_role()
    {
        $roles = (new Admin())->getRole();

        return $this->success($roles);
    }


    // 删除不能删除超级用户
    protected function delWhere()
    {
        $id = $this->id();
        if(empty($id)){
            return $this->_returnFail('请指定要删除的记录！');
        }
        if($id == 1){
            return $this->_returnFail('第一个管理员不能被删除！');
        }

        return ['id' => $id];
    }


    // 添加
    protected function _before_add()
    {
        $opt  = $this->option();
        $data = [
            'nickname' => $opt->getData('nickname', null, 'trim'),
            'username' => $opt->getData('username', null, 'trim'),
            'password' => $opt->getData('password', '', 'trim'),
            'mobile'   => $opt->getData('mobile', null, 'trim'),
            'role'     => $opt->getData('role', null, 'is_array'),
            'status'   => $opt->getData('status', 1, 'intval'),
        ];
        //$this->throwReturn(count($data['role']) != count($data['role'], 1), '权限必须是一维数组！');

        // 检查唯一性
        $id    = $this->id();
        $model = new Admin();
        $data  = $model->checkUser($data, $id);
        if($data === false){
            return $this->_returnFail($model->getError());
        }

        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $opt  = $this->option();
        $data = [
            'username' => $opt->getData('username', null, 'trim'),
            'nickname' => $opt->getData('nickname', null, 'trim'),
            'password' => $opt->getData('password', '', 'trim'),
            'mobile'   => $opt->getData('mobile', null, 'trim'),
            'role'     => $opt->getData('role', null, 'is_array'),
            'status'   => $opt->getData('status', 1),
        ];

        //$this->throwReturn(count($data['role']) != count($data['role'], 1), '权限必须是一维数组！');
        // 检查唯一性
        $id    = $this->id();
        $model = new Admin();
        $data  = $model->checkUser($data, $id);
        if($data === false){
            return $this->_returnFail($model->getError());
        }

        return $data;
    }

}


